using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using PrincetonSUG.UserGroupSite.Helpers;

namespace PrincetonSUG.UserGroupSite.Features.PrincetonSUG.UserGroup
{
  /// <summary>
  /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
  /// </summary>
  /// <remarks>
  /// The GUID attached to this class may be used during packaging and should not be modified.
  /// </remarks>

  [Guid("5b21fb89-8371-4076-bdd0-a100adbd8792")]
  public class PrincetonSUGEventReceiver : SPFeatureReceiver
  {
    // Uncomment the method below to handle the event raised after a feature has been activated.
		public override void FeatureActivated(SPFeatureReceiverProperties properties)
		{
			SPWeb web = ((SPSite)properties.Feature.Parent).RootWeb;

			SPList speakerList = web.Lists.TryGetList("Speakers");
			if (speakerList == null)
			{
				CreateSpeakerList(web);
			}

			if (web.Lists.TryGetList("Meetings") == null)
			{
				CreateMeetingList(web, speakerList);
			}

			if (web.Lists.TryGetList("Sponsors") == null)
			{
				CreateSponsorList(web);
			}
		}

		private static void CreateSponsorList(SPWeb web)
		{
			SPList sponsorList =
				SPWebHelper.EnsureList(web, "Sponsors", "User group sponsors", SPListTemplateType.GenericList);
			SPField title = sponsorList.Fields["Title"];
			title.Title = "Sponsor Name";
			title.Update();

			sponsorList.Fields.Add("Logo", SPFieldType.URL, false);
			sponsorList.Fields.Add("StartDate", SPFieldType.DateTime, true);
			sponsorList.Fields.Add("EndDate", SPFieldType.DateTime, true);
			sponsorList.Update();

			SPView view = sponsorList.DefaultView;
			view.ViewFields.Add("Logo");
			view.ViewFields.Add("StartDate");
			view.ViewFields.Add("EndDate");
			view.Update();
		}

		private static void CreateMeetingList(SPWeb web, SPList speakerList)
		{
			SPList meetingList =
				SPWebHelper.EnsureList(web, "Meetings", "User group meetings", SPListTemplateType.Events);
			SPFieldLookup speakerLookup =
				SPWebHelper.EnsureFieldLookup(
					web, Guid.NewGuid(), "User group fields", "Speaker", speakerList.ID, false
				);
			meetingList.Fields.Add(speakerLookup);
			meetingList.Update();
		}

		private static SPList CreateSpeakerList(SPWeb web)
		{
			SPList speakerList =
				SPWebHelper.EnsureList(web, "Speakers", "User group speakers", SPListTemplateType.GenericList);
			SPField title = speakerList.Fields["Title"];
			title.Title = "Speaker Name";
			title.Update();
			speakerList.Fields.Add("Bio", SPFieldType.Text, false);
			speakerList.Fields.Add("Picture", SPFieldType.URL, false);
			speakerList.Update();

			SPView view = speakerList.DefaultView;
			view.ViewFields.Add("Bio");
			view.ViewFields.Add("Picture");
			view.Update();

			return speakerList;
		}

    // Uncomment the method below to handle the event raised before a feature is deactivated.
    //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    //{
    //}

    // Uncomment the method below to handle the event raised when a feature is upgrading.
    //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
    //{
    //}
  }
}
